/**
 * @date 4:32:32 PM
 * EducationAdminController.java
 * Administrator
 * TODO
 */
package com.qs.education.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qs.education.bean.beans.EducationAdminResponse;
import com.qs.education.bean.entity.EducationAdmin;
import com.qs.education.bean.enums.LoginStatus;
import com.qs.education.bean.utils.VerifyCode;
import com.qs.education.service.IAdminService;

/**
 * @class EducationAdminController
 * @author Brooks
 * @description 
 * @date Oct 17, 2018
 */

@Controller
public class AdminController {
	@Autowired
	private IAdminService iAdminService;
	
	private final Logger log = Logger.getLogger(this.getClass());
	/**
	 * @class 根据用户名和密码登录后台
	 * @author Brooks
	 * @description 
	 * @date Oct 17, 2018
	 */
	
	@RequiresPermissions(value={"user:view"})
	@RequestMapping("/getAdmin")
	public ModelAndView getAdmin(HttpServletRequest request,EducationAdmin admin){
		log.info("后台管理相应");
		ModelAndView mv=new ModelAndView("/admin/index");
		EducationAdminResponse educationAdmin=iAdminService.getEducationAdmin(admin.getName(), admin.getPassword());
		request.getSession().setAttribute("ADMIN", educationAdmin);
		mv.addObject("educationAdmin", educationAdmin);
		return mv;
	}
	
	@RequestMapping("/login")
	public ModelAndView login(){
		ModelAndView mv=new ModelAndView("/login");
		return mv;
	}
	
	@RequestMapping("/userLogin")
	public ModelAndView userLogin(HttpServletRequest request,EducationAdmin admin,String verifyCode,ModelAndView modelAndView){
		String serverCode = (String)request.getSession().getAttribute("serverCode");
		if(serverCode!=null && serverCode.equalsIgnoreCase(verifyCode)) {
			UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(admin.getName(),admin.getPassword());
			//UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(name,password);
			Subject subject = SecurityUtils.getSubject();
			try {
				subject.login(usernamePasswordToken);
				//EducationAdmin user = (EducationAdmin) subject.getPrincipal();
				//request.getSession().setAttribute("SessionUser", user);
				
				modelAndView.addObject("message", LoginStatus.SUCCESS.getLoginStatus());
				modelAndView.setViewName("forward:/getAdmin");
			}catch (UnknownAccountException e) {  
	            
				modelAndView.addObject("message", LoginStatus.UNKNOWNACCOUNT.getLoginStatus());
				modelAndView.setViewName("/login");
	        }catch(IncorrectCredentialsException e){  
	           
	        	modelAndView.addObject("message", LoginStatus.INCORRECTCREDENTIALS.getLoginStatus());
	        	modelAndView.setViewName("/login");
	        }catch (Exception e) {  
	        	modelAndView.addObject("message", LoginStatus.FAILED.getLoginStatus());
	        	modelAndView.setViewName("/login");
	        }  
		}else {
			modelAndView.addObject("message", LoginStatus.VERIFYCODEERROR.getLoginStatus());
			modelAndView.setViewName("/login");
		}
		return modelAndView;
	}
	
	/**
	 * 获取验证码
	 * @author Jason
	 * @throws IOException 
	 * @date Nov 1, 2018
	 */
	@RequestMapping("getVerifyCode")
	public void getVerifyCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
		//获取验证码
		//1 获取验证码图片
		VerifyCode vf = new VerifyCode();
		BufferedImage image =vf.getImage();
		//2 得到验证码的四个随机数
		String code = vf.getText();
		//为了进行验证码校验
		request.getSession().setAttribute("serverCode", code);
		//3 把验证码输出到页面
		VerifyCode.output(image, response.getOutputStream());
		
	}
	
	/**
	 * @Description 跳转到修改密码页面
	 * @author Jason
	 * @date Nov 15, 2018
	 * @return
	 */
	@RequestMapping("/changePasswordHTML")
	public ModelAndView changePasswordHTML(){
		
		log.info("跳转到修改密码界面");
		ModelAndView mv=new ModelAndView("admin/changepassword");
		
		return mv;
	}
	
	/**
	 * @Description
	 * @author Jason
	 * @date Nov 15, 2018
	 * @return
	 */
	@RequestMapping("/updateAdmin")
	@RequiresPermissions(value={"user:update"})
	public ModelAndView updateAdmin( HttpServletRequest request,String name ,String oldPassword ,String newPassword){
		
		log.info("管理员修改密码");
		ModelAndView mv=new ModelAndView("redirect:/logout");
		EducationAdminResponse response=iAdminService.updateAdmin(name,oldPassword,newPassword);
		request.getSession().removeAttribute("ADMIN");
		
		return mv;
	}
	
	
}
